﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bamboo.CodeCat
{
    /// <summary>
    /// 简易代码生成器
    /// </summary>
    public class CodeCat
    {
        public string TemplatePath { get; set; }
        /// <summary>
        /// 新建模板的默认字符集，默认为GBK
        /// </summary>
        public Encoding Encoding { get; set; } = Encoding.GetEncoding("gbk");
        public Dictionary<string, Template> Templates { get; } = new Dictionary<string, Template>();

        public Encoding GBK = Encoding.GetEncoding("gbk");
        public Encoding UTF8WithBOM = new UTF8Encoding(true);
        public Encoding UTF8NoBOM = new UTF8Encoding(false);

        /// <summary>
        /// 添加翻译模板
        /// 如果name填空，则自动以文件名为name。
        /// 如果已存在相同name的模板，会保留旧的，忽略新的。
        /// </summary>
        /// <param name="template"></param>
        public void AddTemplate(string name, string filename, Type templateType)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = System.IO.Path.GetFileName(filename);
            }
            if (Templates.ContainsKey(name)) return;
            var t = new Template(name, filename, this.Encoding, templateType);
            Templates.Add(t.Name, t);
        }

        public string ReplaceToText(string name, object instance)
        {
            var t = GetTemplate(name, instance.GetType());
            var s = t.ReplaceToText(instance);
            return s;
        }

        public void ReplaceToFile(string name, object instance, string filename)
        {
            var t = GetTemplate(name, instance.GetType());
            t.ReplaceToFile(instance, filename);
        }

        public static string ReplaceToText(string moduleFilename, Encoding encoding, object instance)
        {
            var t = new Template("", moduleFilename, encoding, instance.GetType());
            var s = t.ReplaceToText(instance);
            return s;
        }

        public static void ReplaceToFile(string moduleFilename, Encoding encoding, object instance, string outputFilename)
        {
            var t = new Template("", moduleFilename, encoding, instance.GetType());
            t.ReplaceToFile(instance, outputFilename);
        }

        public Template GetTemplate(string name)
        {
            return GetTemplate(name, null);
        }

        public Template GetTemplate(string name, Type templateType)
        {
            Template ret;
            if (Templates.TryGetValue(name, out ret))
            {
                return ret;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(TemplatePath))
                    throw new Exception("根据名称找不到指定的模板，且未设置模板目录，所以也无法自动加载模板，名称：" + name);
                string fullname = System.IO.Path.GetFullPath(System.IO.Path.Combine(TemplatePath, name));
                if (!System.IO.File.Exists(fullname))
                    throw new Exception("根据名称找不到指定的模板，且自动加载模板失败，找不到模板文件：" + fullname);
                ret = new Template(name, fullname, this.Encoding, templateType);
                Templates.Add(name, ret);
                return ret;
            }
        }

        public void WriteFile(string filename, string content, Encoding encoding)
        {
            var file = new System.IO.FileInfo(filename);
            if (file.Directory.Exists == false)
            {
                try
                {   //防止并发异常
                    file.Directory.Create();
                }
                catch (Exception) { }
            }

            System.IO.File.WriteAllText(filename, content, encoding);
        }
    }

}
